$(function () {
    new FastClick(document.body);
    //节流阀
    function loadActivity() {
        if (flag) return false;
        $.ajax({
            url: domain.test + "/getactivity",
            // url: domain.online + "/api/activity/list",
            type: 'get',
            data: "pageNum=" + pageNum,
            dataType: 'json',
            beforeSend: function () {
                flag = true;
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200){
                    var activityDetail = response.data.data;
                    // var activityDetail = response.data;
                    console.log(activityDetail.list);
                    var secondArr = [];
                    $.each(activityDetail.list, function (index, value) {
                        secondArr.push(Math.abs(value.second));
                    });
                    // secondArr[1] = 20000;
                    if (pageNum === 1) {
                        $('.activityItemBox').html(template('activityItemBox', { activityDetail: activityDetail }));
                    } else {
                        $('.activityItemBox').append(template('activityItemBox', { activityDetail: activityDetail }));
                    }
                    pullmaster.pullDownSuccess();       //刷新成功
                    if (activityDetail.list.length <= 10 || activityDetail.list.length === 0) {
                        pullmaster.pullUpDone();
                        $(".bb_pull-up").css({ "display": "none" });
                        $(".end").css({ "display": "block" });
                    }
                    pageNum++;
                    flag = false;
                    $.each(secondArr, function (index, value) {
                        if (typeof value === "number") {
                            console.log(value);
                            if (value > 0) {
                                var timer = setInterval(function () {
                                    value -= 1000;
                                    if (value < 1000) {
                                        clearInterval(timer);
                                        loadActivity();
                                    }
                                    var hour = Math.floor(value / 1000 / 3600);
                                    var minute = Math.floor(value / 1000 / 60 % 60);
                                    var second = Math.floor(value / 1000 % 60);
                                    hour = hour >= 10 ? hour : "0" + hour;
                                    minute = minute >= 10 ? minute : "0" + minute;
                                    second = second >= 10 ? second : "0" + second;
                                    $($(".activityItem")[index]).find(".hour").html(hour);
                                    $($(".activityItem")[index]).find(".minute").html(minute);
                                    $($(".activityItem")[index]).find(".second").html(second);
                                }, 1000);
                            }
                        }
                    });
                }
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        })
    }
    var flag = false;
    var pageNum = 1;
    var pullmaster = new Pull($('.activityItemBox'), {
        onPullUp: function () {
            pageNum = 1;
            loadActivity();
        },
        onPullDown: function () {
            pageNum = 1;
            loadActivity();
        }
    });
    //活动详情
    $(".activityItemBox").on("click", ".activityItem", function () {
        var id = $(this).attr("data-id");
        location.href = "./activityDetail.html?id=" + id;
    })
})